package tiago.web_services;


/**
 * 
 * Activity to use Web Services in Android.
 * This API uses the KSOAP 2 version 2.3.
 * 
 * This project call a function that convert one temperature typed by the user
 * from Celsius to Fahrenheit. The link of the Web Serivce is:
 * http://www.w3schools.com/webservices/tempconvert.asmx
 * 
 * Next step is update KSOAP 2 to latest version(2.6).
 * 
 * 
 * @author Tiago Melo
 *
 */

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class WebServiceInternet extends Activity implements OnClickListener
{
	//Name of the SOAP ACTION. (Can be extracted from http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit)
	private String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
	//Name of the method used by application
	private String METHOD_NAME = "CelsiusToFahrenheit";
	//Name of namespace
	private String NAMESPACE = "http://tempuri.org/";
	//URL of the site of Web Service
	private String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

	//GUI components
	EditText editTextCelsiusTemperature, editTextResultOfConversion;
	Button buttonConvert;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		//Defaul code generated by Eclipse
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//Initialize GUI components,events,actions,etc.
		initializeGUIComponents();
	}

	/**
	 * Function that initialize the GUI components
	 */
	private void initializeGUIComponents()
	{
		editTextCelsiusTemperature = (EditText) findViewById(R.id.editText1);
		
		editTextResultOfConversion = (EditText) findViewById(R.id.editText2);
		
		buttonConvert = (Button) findViewById(R.id.buttonConvert);
		
		buttonConvert.setOnClickListener(this);
	}

	/**
	 * Function that do all operations with Web Service
	 */
	private void callWebService()
	{
		//Request object
		SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

		//Add the name of parameter("Celsius")
		Request.addProperty("Celsius", editTextCelsiusTemperature.getText()
				.toString());

		//Here is possible choose the verstion of Web Services (1.0, 1.1 or 1.2)
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		
		//If the server is written in .net this flag MUST BE true. Otherwise the application
		//can�t be the right behaviour. For other servers(PHP,java,etc) this value MUST BE false.
		soapEnvelope.dotNet = true;
		
		soapEnvelope.setOutputSoapObject(Request);

		HttpTransportSE aht = new HttpTransportSE(URL,10000);

		try
		{
			//Excecute the call for the server.
			aht.call(SOAP_ACTION, soapEnvelope);
			
			//Get the result of the call.
			SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
					.getResponse();

			//Put the result on the EditText
			editTextResultOfConversion.setText("" + resultString.toString());

		} catch (Exception e)
		{
			//Put the message of exception in EditText
			editTextResultOfConversion.setText("Exception: " + e.getMessage());
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	public void onClick(View v)
	{
		//If the component clicked is the buttonConvert
		if (v == buttonConvert)
		{
			//Call Web Service to do the conversion.
			callWebService();
		}

	}

}
